package com.mall.server.controller;

import com.mall.commons.R;
import com.mall.commons.exceotion.BusinessException;
import com.mall.commons.exceotion.ErrorResult;
import com.mall.server.service.UmsResourceService;
import com.tanhua.model.domain.UmsResource;
import com.tanhua.model.dto.ResourceListDao;
import com.tanhua.model.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/resource")
public class UmsResourceController {

    @Autowired
    private UmsResourceService umsResourceService;

    /**
     * 分页模糊查询后台资源
     *
     * @param resourceListDao
     * @return
     */
    @GetMapping("/list")
    public R listResource(ResourceListDao resourceListDao) {

        PageVo vo = umsResourceService.listResource(resourceListDao);

        if (vo == null) {
            throw new BusinessException(ErrorResult.emptyError());
        }
        return R.success(vo);
    }

    /**
     * 根据ID删除后台资源
     *
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public R delete(@PathVariable Integer id) {
        umsResourceService.delete(id);
        return R.success("操作成功");
    }

    /**
     * 修改后台资源
     *
     * @param id
     * @param umsResource
     * @return
     */
    @PostMapping("/update/{id}")
    public R update(@PathVariable Integer id, @RequestBody UmsResource umsResource) {
        umsResourceService.update(id, umsResource);
        return R.success("操作成功");
    }

    /**
     * 添加后台资源
     *
     * @param umsResource
     * @return
     */
    @PostMapping("/create")
    public R create(@RequestBody UmsResource umsResource) {
        umsResourceService.create(umsResource);
        return R.success("操作成功");
    }


    /**
     * 查询所有后台资源
     *
     * @return
     */
    @GetMapping("/listAll")
    public R listAll() {
        List<UmsResource> resource = umsResourceService.listAll();
        return R.success(resource);
    }
}
